package com.health.viewscontrols.medical.device.bg;

import android.os.Handler;
import android.util.Log;
import android.view.View;

import com.clj.fastble.utils.HexUtil;
import com.health.viewscontrols.medical.base.BaseDeviceActivity;
import com.nova.health.ble.callback.OnDeviceConnectChangedListener;
import com.novabluetooth.data.DeviceShowSaveData;

import java.math.BigDecimal;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;

/**
 * 百捷三合一
 */
public class BJSHYActivity extends BaseDeviceActivity {

    public final static UUID UUID_SERVICE =
            UUID.fromString("00001000-0000-1000-8000-00805F9B34FB");//蓝牙设备的Service的UUID --0000ffe0
    public final static UUID UUID_NOTIFY =
            UUID.fromString("00001002-0000-1000-8000-00805F9B34FB");//蓝牙设备的Characteristic的UUID --0000ffe1
    private BigDecimal jcresult = new BigDecimal(0l);

    @Override
    public void doBusiness() {
        tv_operation_tips.setText(com.nova.health.ble.R.string.baijie);
        mBleDevice.setDevice_Service_UUID(UUID_SERVICE);
        mBleDevice.setDevice_Notify_UUID(UUID_NOTIFY);
        mBleDevice.setConnectChangedListener(new OnDeviceConnectChangedListener() {
            @Override
            public void onConnected() {
                Log.e(TAG, "onConnected: ");
                isConnected = true;
                runOnUiThread(() -> {
                    blueToothState.setText("蓝牙已连接");
                    showProgressDialog("等待接收数据" + deviceEnum.getTitle() + "数据");
                });
            }

            @Override
            public void onDisconnected() {
                isConnected = false;
                Log.e(TAG, "onDisconnected: ");
                cancelProgressDialog();
                runOnUiThread(() -> {
                    blueToothState.setText("蓝牙连接失败");
                });
            }
        });
    }

    @Override
    public void startDevice() {
        if (isConnected) {
            blueToothState.setText("蓝牙已连接");
            showProgressDialog("等待接收数据" + deviceEnum.getTitle() + "数据");
        } else {
            blueToothState.setText("蓝牙开始连接");
            showProgressDialog("正在连接" +  deviceEnum.getTitle());
            mBleDevice.setConnect(true);
            mBleDevice.setMAC(deviceEnum.getMac());
            mBleDevice.connect(deviceEnum.getMac());
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    mBleDevice.setConnect(true);
                    mBleDevice.setMAC(deviceEnum.getMac());
                    mBleDevice.connect(deviceEnum.getMac());
                }
            },3200);
        }
    }

    @Override
    public void update(String hexStr) {
        Log.e(TAG, "update: " + hexStr );

        try {
            byte[] data = HexUtil.hexStringToBytes(hexStr);
            formatData(data);
            Log.e(TAG, "update: " + data.length );
            disconnectDevice();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void formatData(byte[] data) {
        //接收数据
        try {
            String formatHexString1 = HexUtil.formatHexString(data);
            String type = formatHexString1.substring(8, 10);
            String data1 = formatHexString1.substring(
                    formatHexString1.length() - 4,
                    formatHexString1.length() - 2);

            String data2 = formatHexString1.substring(
                    formatHexString1.length() - 6,
                    formatHexString1.length() - 4);
            int result = hexStringToInt(data1+ data2);
            tv_state.setText("获取测量结果成功");
            switch (type) {
                case "41":
                    //血糖
                    BigDecimal bg = new BigDecimal(result/18.0);
                    tv_result.setText("血糖测量结果：\n        " + bg.setScale(2, BigDecimal.ROUND_HALF_UP) +"mmol/L");
                    if (jcresult.compareTo(bg.setScale(2, BigDecimal.ROUND_HALF_UP)) == 0) {
                        return;
                    }
                    jcresult = bg.setScale(2, BigDecimal.ROUND_HALF_UP);
                    Log.e("xiaoxin","测量结果: "+"--" + jcresult);
                    tv_result.setText("血糖值： " + jcresult  + "mmol/L");
                    DeviceShowSaveData.getDataShow(jcresult +"", "BeneCheck");
                    DeviceShowSaveData.saveXuetangBj(jcresult +"");
                    break;
                case "51":
                    //尿酸设备
                    BigDecimal ns = new BigDecimal((result * 0.1)/16.81 * 1000);
                    tv_result.setText("尿酸测量结果：\n        " + ns.setScale(2, BigDecimal.ROUND_HALF_UP) +"μmol/L");
                    if (jcresult.compareTo(ns.setScale(2, BigDecimal.ROUND_HALF_UP)) == 0) {
                        return;
                    }
                    jcresult = ns.setScale(2, BigDecimal.ROUND_HALF_UP);
                    tv_result.setText("尿酸值： " + jcresult  + "μmol/L");
                    Log.e("xiaoxin","尿酸设备测量结果: "+"--" + jcresult);
                    DeviceShowSaveData.getDataShow(jcresult +"", "BeneCheck-ns");
                    DeviceShowSaveData.saveNs(jcresult +"");
                    break;
                case "61":
                    //胆固醇设备
                    BigDecimal dgc = new BigDecimal(result/38.66);
                    tv_result.setText("胆固醇测量结果：\n        " + dgc.setScale(2, BigDecimal.ROUND_HALF_UP) +"mmol/L");
                    if (jcresult.compareTo(dgc.setScale(2, BigDecimal.ROUND_HALF_UP)) == 0) {
                        return;
                    }
                    jcresult = dgc.setScale(2, BigDecimal.ROUND_HALF_UP);
                    tv_result.setText("胆固醇值： " + jcresult  + "mmol/L");
                    Log.e("xiaoxin","胆固醇设备测量结果: "+"--" + jcresult);
                    DeviceShowSaveData.getDataShow(jcresult +"", "BeneCheck-dgc");
                    DeviceShowSaveData.saveDgc(jcresult +"");
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static int hexStringToInt(String HexString) {
        int inJTFingerLockAddress = Integer.valueOf(HexString, 16);
        return inJTFingerLockAddress;
    }

    @Override
    public void disconnectDevice() {
        try {
            cancelProgressDialog();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            mBleDevice.setConnect(false);
            mBleDevice.disconnect();
            mBleDevice.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
